package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderDao orderDao;

    @Autowired
    MemberDao memberDao;

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public Result submit(Map<String, Object> map) throws Exception {
        String orderDate = (String) map.get("orderDate");
        String setmealId = (String) map.get("setmealId");
        OrderSetting orderSetting=orderSettingDao.findOrderSettingByOrderDate(DateUtils.parseString2Date(orderDate));
        if(orderSetting==null){
            //当前日期没有安排体检,无法预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if(reservations>=number){
            //当前日期预约人数已满,无法预约
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        Member member = new Member();
        member.setName(map.get("name").toString());
        member.setSex(map.get("sex").toString());
        member.setPhoneNumber(map.get("telephone").toString());
        member.setIdCard(map.get("idCard").toString());
        Integer memberID = memberDao.find(member);
        Order order = new Order(memberID,DateUtils.parseString2Date(orderDate),Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO,Integer.parseInt(setmealId));
        if(memberID==null){
            //不是会员,注册会员
            memberDao.add(member);
            order.setMemberId(member.getId());

        }else {
            //是会员,判断有没有重复预约
            Integer i = orderDao.find(order);
            if(i>0){
                //重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }

        }
        //可以预约,往订单表添加数据,并且把订单设置表的reservations字段加1
        orderDao.add(order);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.updateByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map<String, Object> findById(String id) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map=orderDao.findById(id);
        Date orderDate = (Date) map.get("orderDate");
        map.put("orderDate",DateUtils.parseDate2String(orderDate));
        return map;
    }

    @Override
    public Map<String, Object> getSetmealReport() {
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String,Object>> countList= orderDao.getOrderCount();
        List<String> nameList = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : countList) {
            String name = (String) stringObjectMap.get("name");
            nameList.add(name);
        }
        map.put("setmealName",nameList);
        map.put("setmealCount",countList);
        return map;
    }
}
//{"data":{"setmealName":["入职无忧体检套餐（男女通用）","粉红珍爱(女)升级TM12项筛查体检套餐","阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐","珍爱高端升级肿瘤12项筛查（男女单人）"],
//        "setmealCount":[{"COUNT(*)":3,"name":"入职无忧体检套餐（男女通用）"},
//                        {"COUNT(*)":2,"name":"粉红珍爱(女)升级TM12项筛查体检套餐"},
//                        {"COUNT(*)":2,"name":"阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐"},
//                        {"COUNT(*)":2,"name":"珍爱高端升级肿瘤12项筛查（男女单人）"}]},
//
//        "flag":true,"message":"获取套餐统计数据成功"}